/*
 * Created by JFormDesigner on Wed Dec 20 01:22:54 CST 2023
 */

package cn.xiaobai.controller;

import java.awt.event.*;

import cn.xiaobai.domain.Message;
import cn.xiaobai.service.AuthorServer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author dell
 */
public class Login extends JDialog {

    private static AuthorServer authorServer;

    /**
     * 数据库配置是否初始化
     */
    private final AtomicBoolean db_init = new AtomicBoolean(false);

    public Login() {
        setModalityType(ModalityType.APPLICATION_MODAL);
        authorServer = new AuthorServer();
        initComponents();
    }


    /**
     * 登录触发
     *
     * @param e
     */
    private void login_bt_MouseClicked(MouseEvent e) {

        String username = this.username.getText();
        String password = this.password.getText();
        Message message = authorServer.login(username, password);
        if (message.isState()) {
            JOptionPane.showMessageDialog(this, message.getMsg(), "消息", JOptionPane.INFORMATION_MESSAGE);
            dispose();

            test test = new test();
            test.display();
        } else {
            JOptionPane.showMessageDialog(this, message.getMsg(), "消息", JOptionPane.ERROR_MESSAGE);
        }

    }

    private void regis_bt_MouseClicked(MouseEvent e) {
        String username = this.username.getText();
        String password = this.password.getText();
        Message message = authorServer.regis(username, password);
        if (message.isState()) {
            JOptionPane.showMessageDialog(this, message.getMsg(), "消息", JOptionPane.INFORMATION_MESSAGE);
        } else {
            JOptionPane.showMessageDialog(this, message.getMsg(), "消息", JOptionPane.ERROR_MESSAGE);
        }
    }


    private void windowOpened(WindowEvent e) {
        // 窗体激活处理
        if (!db_init.get()) {
            Message message = authorServer.checkConn();
            if (!message.isState()) {
                JOptionPane.showMessageDialog(this, message.getMsg(), "消息", JOptionPane.ERROR_MESSAGE);
                System.exit(0);
            } else {
                db_init.set(true);
            }
        }
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        panel2 = new JPanel();
        regis_bt = new JButton();
        login_bt = new JButton();
        pass_text = new JLabel();
        username = new JTextField();
        password = new JTextField();
        username_text = new JLabel();

        //======== this ========
        setTitle("LOGIN");
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowOpened(WindowEvent e) {
                Login.this.windowOpened(e);
            }
        });
        var contentPane = getContentPane();

        //======== panel2 ========
        {

            //---- regis_bt ----
            regis_bt.setText("regis");
            regis_bt.setBackground(new Color(0x549cfa));
            regis_bt.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    regis_bt_MouseClicked(e);
                }
            });

            //---- login_bt ----
            login_bt.setText("login");
            login_bt.setBackground(new Color(0x549cfa));
            login_bt.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    login_bt_MouseClicked(e);
                }
            });

            //---- pass_text ----
            pass_text.setText("\u5bc6\u7801:");
            pass_text.setBackground(new Color(0x3b9399));
            pass_text.setForeground(new Color(0x65b1ff));

            //---- username ----
            username.setSelectedTextColor(new Color(0x2c2c34));
            username.setSelectionColor(new Color(0x2c2c34));

            //---- username_text ----
            username_text.setText("\u8d26\u6237:");
            username_text.setBackground(new Color(0x3b9399));
            username_text.setForeground(new Color(0x65b1ff));

            GroupLayout panel2Layout = new GroupLayout(panel2);
            panel2.setLayout(panel2Layout);
            panel2Layout.setHorizontalGroup(
                panel2Layout.createParallelGroup()
                    .addGroup(GroupLayout.Alignment.TRAILING, panel2Layout.createSequentialGroup()
                        .addContainerGap(84, Short.MAX_VALUE)
                        .addGroup(panel2Layout.createParallelGroup()
                            .addGroup(panel2Layout.createSequentialGroup()
                                .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                                    .addComponent(pass_text, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(username_text, GroupLayout.PREFERRED_SIZE, 60, GroupLayout.PREFERRED_SIZE))
                                .addGap(28, 28, 28)
                                .addGroup(panel2Layout.createParallelGroup()
                                    .addComponent(username, GroupLayout.PREFERRED_SIZE, 188, GroupLayout.PREFERRED_SIZE)
                                    .addComponent(password, GroupLayout.PREFERRED_SIZE, 188, GroupLayout.PREFERRED_SIZE)))
                            .addGroup(panel2Layout.createSequentialGroup()
                                .addGap(16, 16, 16)
                                .addComponent(login_bt)
                                .addGap(69, 69, 69)
                                .addComponent(regis_bt)))
                        .addGap(68, 68, 68))
            );
            panel2Layout.setVerticalGroup(
                panel2Layout.createParallelGroup()
                    .addGroup(panel2Layout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(username_text, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
                            .addComponent(username, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(pass_text, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
                            .addComponent(password, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                        .addGap(54, 54, 54)
                        .addGroup(panel2Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                            .addComponent(regis_bt)
                            .addComponent(login_bt))
                        .addContainerGap(43, Short.MAX_VALUE))
            );
        }

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(16, 16, 16)
                    .addComponent(panel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(19, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(30, 30, 30)
                    .addComponent(panel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(35, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JPanel panel2;
    private JButton regis_bt;
    private JButton login_bt;
    private JLabel pass_text;
    private JTextField username;
    private JTextField password;
    private JLabel username_text;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
